package cn.xz.tipsum.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import cn.xz.tipsum.R
import cn.xz.tipsum.adapter.MyTwoListAdapter
import cn.xz.tipsum.bean.ERROR_CODE
import cn.xz.tipsum.bean.EventBusMessage
import cn.xz.tipsum.bean.GET_MSG
import cn.xz.tipsum.bean.MyBean
import cn.xz.tipsum.databinding.ActivityTwoBinding
import cn.xz.tipsum.dialog.ErrorDialog
import cn.xz.tipsum.utils.EventBusUtils
import cn.xz.tipsum.utils.MyNotificationListenerService
import cn.xz.tipsum.utils.SessionUtils
import org.greenrobot.eventbus.Subscribe
import java.text.SimpleDateFormat
import java.util.*

class TwoActivity : AppCompatActivity() {

    private lateinit var mDataBind: ActivityTwoBinding

    private val mAdapter by lazy { MyTwoListAdapter() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置屏幕常亮
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        EventBusUtils.register(this)
        mDataBind = DataBindingUtil.setContentView(this, R.layout.activity_two)
        initRv()
        initData()
        initEvent()
        startTime()
    }

    private fun initData(){
        mAdapter.setList(
            SessionUtils.getDataList(
                SessionUtils.INPUT_DATA,
            MyBean::class.java) as ArrayList<MyBean>)
        //列表滑到最底
        mDataBind.rvList.postDelayed({
            mDataBind.rvList.scrollToPosition(mAdapter.itemCount - 1)
        },100)
    }

    var timer: Timer? = null
    private fun startTime(){
        stopTime()
        // 创建计时器对象
        timer = Timer()
        val timerTask: TimerTask = object : TimerTask() {
            override fun run(){
                mDataBind.tvTime.post {
                    val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    val curDate = Date(System.currentTimeMillis())
                    mDataBind.tvTime.text = formatter.format(curDate)
                }
            }
        }
        timer?.schedule(timerTask, 0,1000)
    }

    private fun stopTime(){
        if (timer != null){
            timer?.cancel()
            timer = null
        }
    }

    private fun initRv(){
        mDataBind.rvList.apply {
            layoutManager = LinearLayoutManager(this@TwoActivity)
            adapter = mAdapter
            //RecyclerView闪烁问题
            (itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        }
    }

    private fun initEvent(){
        mDataBind.apply {
            tvTime.setOnClickListener {
                finish()
            }
        }
    }

    //添加数据
    private fun addData(mBean: MyBean){
        try {
            mAdapter.addData(mBean)
            //列表滑到最底
            mDataBind.rvList.smoothScrollToPosition(mAdapter.itemCount)
        }catch (e: java.lang.Exception){
            e.printStackTrace()
        }

    }

    @Subscribe
    fun onSubscriberEvent(message: EventBusMessage) {
        when(message.messageTag) {
            GET_MSG -> { //刷新评论
                mDataBind.rvList.post {
                    try {
                        val mB = message.messageBody as MyBean
                        addData(mB)
                    }catch (e: Exception){
                        e.printStackTrace()
                    }
                }
            }
            ERROR_CODE ->{
                mDataBind.tvTime.post {
                    try {
                        val mB = message.messageBody as String
                        ErrorDialog.newInstance(mB).show(this.supportFragmentManager,"ErrorDialog")
                    }catch (e: Exception){
                        e.printStackTrace()
                    }
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBusUtils.unRegister(this)
        stopTime()
    }


}